﻿using GrapeCity.Forguncy.CellTypes;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using Vant.CellTypes;

namespace Vant.WpfControls.DrawingObject
{
    /// <summary>
    /// Interaction logic for CircleDrawingObject.xaml
    /// </summary>
    public partial class CircleDrawingObject : UserControl
    {
        double _value;
        Circle _circle;
        IDrawingHelper _drawingHelper;
        public CircleDrawingObject(Circle circle, CellTypeViewModelStyle cellStyle, ICellInfo cell, IDrawingHelper drawingHelper)
        {
            var viewModel = new CircleDrawingObjectViewModel();
            cellStyle.TextWrapping = TextWrapping.NoWrap;
            _circle = circle;
            _drawingHelper = drawingHelper;
            if (!double.TryParse(cell.Value?.ToString() ?? "0", out var value))
            {
                value = 30;
            }
            if (circle.text != null)
            {
                viewModel.Text = circle.text?.ToString();
            }
            else
            {
                viewModel.Text = Math.Round(value) + "%";
            }
            cellStyle.Foreground = cellStyle.Foreground ?? _drawingHelper.GetBrush("#323233");
            _value = value;
            viewModel.Size = circle.size;
            viewModel.CellStyle = cellStyle;
            this.DataContext = viewModel;
            InitializeComponent();
        }

        protected override void OnRender(DrawingContext drawingContext)
        {
            base.OnRender(drawingContext);
            var size = _drawingHelper.GetImageCellSize();
            var offset = _circle.strokeWidth / 2;
            drawingContext.PushClip(new RectangleGeometry(new Rect(0, 0, size.Width, size.Height), 0,0));
            drawingContext.PushTransform(new TranslateTransform(offset, offset));
            try
            {
                var radius = this._circle.size / 2 - offset;
                if (this._circle.fill != null)
                {
                    var fill = _drawingHelper.GetBrush(this._circle.fill);
                    drawingContext.DrawEllipse(fill, null, new Point(radius, radius), radius, radius);
                }
                drawingContext.DrawEllipse(null, new Pen(_drawingHelper.GetBrush(_circle.layerColor), _circle.strokeWidth), new Point(radius, radius), radius, radius);

                if(_value >= 100)
                {
                    drawingContext.DrawEllipse(null, new Pen(_drawingHelper.GetBrush(_circle.color), _circle.strokeWidth), new Point(radius, radius), radius, radius);
                }
                else
                {
                    PathGeometry path = new PathGeometry();
                    var PathFigure = new PathFigure();
                    var start = new Point(radius, 0);
                    switch (_circle.startPosition)
                    {
                        case "bottom":
                            start = new Point(radius, radius * 2);
                            break;
                        case "left":
                            start = new Point(0, radius);
                            break;
                        case "right":
                            start = new Point(radius * 2, radius);
                            break;
                        default:
                            break;
                    }
                    var center = new Point(radius, radius);
                    var angle = Math.Min(2 * Math.PI, (_value / 100 * 2) * Math.PI);
                    if (!_circle.clockwise)
                    {
                        angle = Math.PI * 2 - angle;
                    }
                    var endX = (start.X - center.X) * Math.Cos(angle) - (start.Y - center.Y) * Math.Sin(angle) + center.X;
                    var endY = (start.X - center.X) * Math.Sin(angle) + (start.Y - center.Y) * Math.Cos(angle) + center.Y;
                    var end = new Point(endX, endY);

                    var direction = SweepDirection.Counterclockwise;
                    var isLargeArc = angle < Math.PI;
                    if (_circle.clockwise)
                    {
                        direction = SweepDirection.Clockwise;
                        isLargeArc = angle > Math.PI;
                    }
                    PathFigure.StartPoint = start;
                    PathFigure.Segments.Add(new ArcSegment(end, new Size(radius, radius), 0, isLargeArc, direction, true));
                    path.Figures.Add(PathFigure);
                    var pen = new Pen(_drawingHelper.GetBrush(_circle.color), _circle.strokeWidth);
                    switch (_circle.strokeLinecap)
                    {
                        case "round":
                            pen.StartLineCap = PenLineCap.Round;
                            pen.EndLineCap = PenLineCap.Round;
                            break;
                        case "square":
                            pen.StartLineCap = PenLineCap.Square;
                            pen.EndLineCap = PenLineCap.Square;
                            break;
                        case "butt":
                            pen.StartLineCap = PenLineCap.Flat;
                            pen.EndLineCap = PenLineCap.Flat;
                            break;
                        default:
                            break;
                    }
                    drawingContext.DrawGeometry(null, pen, path);
                }
            }
            finally
            {
                drawingContext.Pop();
                drawingContext.Pop();
            }
        }
    }
    public class CircleDrawingObjectViewModel : ModelBase
    {
        public int Size { get; set; }
        public CellTypeViewModelStyle CellStyle { get; set; }
        public string Text { get; set; }
    }
}
